<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select
        @change="changeDate"
        v-model="currentYear"
        style="width: 120px"
      >
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        ></el-option>
      </el-select>
      <el-select
        @change="changeDate"
        v-model="currentMonth"
        style="width: 120px; margin-left: 10px"
      >
        <el-option
          v-for="item in 12"
          :key="item"
          :label="item"
          :value="item"
        ></el-option>
      </el-select>
    </el-row>
    <!-- v-model:双向绑定数据 -->
    <el-calendar v-model="currentDate">
      <template slot="dateCell" slot-scope="{ date, data }">
        <div class="date-content">
          <span class="text"> {{ date.getDate() }}</span>
          <span v-if="isWeek(date)" class="rest"> 休 </span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  name: "work-calendar",

  created() {
    let year = this.currentDate.getFullYear();

    this.currentYear = year;
    this.currentMonth = this.currentDate.getMonth() + 1;

    // 从前5年开始遍历，到后5年，依次加到数字里
    // 例如：今年2021，那么从 2016开始，到2026,依次递增加到数组里
    for (let i = year - 5; i <= year + 5; i++) {
      this.yearList.push(i);
    }
  },
  data() {
    return {
      currentDate: new Date(),
      yearList: [],
      currentYear: null,
      currentMonth: null,
    };
  },

  methods: {
    changeDate() {
      // 让日历跟着改变，根据我选择的年、月来改变日历
      // this.currentYear + this.currentMonth
      this.currentDate = new Date(
        `${this.currentYear}-${this.currentMonth}-01`
      );
    },

    isWeek(date) {
      // getDate()获取日（年、月、日里的日）
      // 日期对象有个方法叫 getDay() 获得一周内的第几天
      // 简单来说就是获取星期几，只不过它是从0开始
      // 外国是以星期天为一周的开始，所以星期天为0，星期一就是1，星期2就是2
      // 总而言之就是1-6都是对的，星期天为0
      return date.getDay() == 6 || date.getDay() == 0;
    },
  },
};
</script>

<style  scoped>
/deep/ .el-calendar-day {
  height: auto;
}
/deep/ .el-calendar-table__row td,
/deep/ .el-calendar-table tr td:first-child,
/deep/ .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
/deep/ .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
/deep/ .el-calendar__header {
  display: none;
}

::v-deep .el-input.el-input--suffix input {
  height: 30px;
}
</style>